module.exports = function ($scope, $resource, $modalInstance, toaster, FileUploader) {

    $scope.pageInfo = {};
    $scope.pageInfo.isSelected = false;
    $scope.loadlist = function () {
        $resource('/api/as/ggc/bus/findBusList', {}, {}).save($scope.pageInfo, function (res) {
            if (res.errcode === 0) {
                $scope.noEmptyBusList = res.data;
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
        $resource('/api/as/ggc/activity/findSignupActivityList', {}, {}).save($scope.pageInfo, function (res) {
            if (res.errcode === 0) {
                $scope.activityList = res.data;
                $scope.pageInfo.activity = $scope.activityList[0];
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }
    $scope.loadlist();

    $scope.$watch('pageInfo.activity', function (newValue) {
        if (!$scope.pageInfo.activity) {
            return false;
        }
        let para = {
            tour_date: $scope.pageInfo.activity.tour_date
        }
        $resource('/api/as/ggc/bus/findEmptyBusList', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                $scope.emptyBusList = res.data;
                $scope.pageInfo.emptyBusSeats = $scope.emptyBusList[0].seats;
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }, true);

    $scope.busList = [];
    $scope.addEmptyBus = function (seats) {
        $scope.pageInfo.isSelected = true;
        if (!$scope.pageInfo.activity) {
            toaster.error({ title: '', body: '请选择活动' });
            return false;
        }
        let companyList = angular.copy($scope.pageInfo.activity.companyList);
        $scope.busList.push({
            name: `空车${seats}座`,
            seats: seats,
            companyList: companyList
        })
    }

    $scope.delete = function (index) {
        $scope.busList.splice(index, 1);
    }

    $scope.ok = function () {
        $scope.pageInfo.list = [];
        let total_num = 0;
        for (var index = 0; index < $scope.busList.length; index++) {
            var element = $scope.busList[index];
            for (let i = 0; i < element.companyList.length; i++) {
                const company = element.companyList[i];
                if (company.num > company.user_num || company.num > element.seats) {
                    toaster.error({ title: '人数有误', body: '部门上车人数大于部门报名人数或者车座位数' });
                    return false;
                } else {
                    total_num += company.num;
                }
            }
        }
        if (!$scope.pageInfo.address) {
            toaster.error({ title: '', body: '请填写上车地点' });
            return false;
        }
        if (!$scope.pageInfo.start_time) {
            toaster.error({ title: '', body: '请填写发车时间' });
            return false;
        }
        if (total_num != $scope.pageInfo.activity.user_num) {
            toaster.error({ title: '人数有误', body: '上车人数不等于您公司报名人数' });
            return false;
        }
        if (!confirm('请认真核对信息，是否确定发车。发车后您单位将不能再报名当前日期此活动，已报名人员也将不能更改！！！')) {
            return false;
        }
        let para = {
            activity_id: $scope.pageInfo.activity.activity_id,
            tour_date: $scope.pageInfo.activity.tour_date,
            address: $scope.pageInfo.address,
            start_time: $scope.pageInfo.start_time,
            total_num: total_num,
            list: $scope.busList
        }
        $resource('/api/ac/ggc/voluntourismBusService/updateBusCompany', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '分配成功' });
                $modalInstance.close();
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    }

};